﻿using System.Data;
using System.Data.SqlClient;
using Uber.Core.Repository;

namespace Uber.Data
{
    public class BannedIpRepo : BaseRepo, IBannedIpRepo
    {
        public BannedIpRepo(IConnectionFactory connFactory) : base(connFactory)
        {
        }

        public int Insert(string ip)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "insertBannedIp";
                    cmd.Parameters.Add("ip", SqlDbType.NVarChar, 39).Value = ip;
                    conn.Open();

                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public int Count(string ip)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getBannedIpCount";
                    cmd.Parameters.Add("ip", SqlDbType.NVarChar, 39).Value = ip;
                    conn.Open();

                    return (int)cmd.ExecuteScalar();
                }
            }
        }

        public int Delete(string ip)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "deleteBannedIp";
                    cmd.Parameters.Add("ip", SqlDbType.NVarChar, 39).Value = ip;
                    conn.Open();

                    return cmd.ExecuteNonQuery();
                }
            }
        }

    }
}